<?php

namespace App\Admin\Controllers;

use App\Admin\Forms\ArchiveSettingForm;
use App\Admin\Forms\NotePlanForm;
use App\Admin\Forms\SiteSettingForm;
use App\Admin\Grid\Tools\RefreshMediaButton;
use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use App\Models\Administrator;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index(Content $content): Content
    {
        return $content
            ->header('Dashboard')
            ->description('Description...')
            ->body(function (Row $row) {
                $user = Admin::user();
                $row->column(6, function (Column $column) use ($user) {
                    $params = [
                        'greeting' => greetOfNowCn(),
                        'username' => $user->name,
                        'avatar' => $user->getAvatar(),
                    ];
                    $title = view('admin::dashboard.title', $params);
                    $column->row($title);
                });
                $row->column(6, function (Column $column) use ($user) {
                    $type = $user->can("clues.check_all") ? 1 : ($user->isRole(Administrator::DISPATCH_KEY) ? 2 : 0);
                    if ($type) {
                        $column->row(new Examples\NewUsers($type));
                    }
                });
                if ($user->can('note-manager'))
                    $row->column(6, new Card(null, new NotePlanForm()));
            });
    }

    public function setting(Content $content): Content
    {
        return $content
            ->header('系统配置')
            ->body(new Card(new SiteSettingForm()));
    }

    public function archiveSetting(Content $content): Content
    {
        return $content
            ->header('建档配置')
            ->body(function (Row $row) {
                $btn = new RefreshMediaButton();
                $row->column(3, $btn);
            })
            ->body(new Card(new ArchiveSettingForm()));
    }


    public function getCity(Request $request)
    {
        $q = $request->get('q');

        return response()
            ->json(
                $q ? getCity($q)->map(function ($item) {
                    return [
                        'id' => $item['code'],
                        'text' => $item['name'],
                    ];
                })->toArray() : []
            );
    }

    public function getArea(Request $request): \Illuminate\Http\JsonResponse
    {
        $q = $request->get('q');
        return response()
            ->json(
                $q ? getDistrict($q)->map(function ($item) {
                    return [
                        'id' => $item['code'],
                        'text' => $item['name'],
                    ];
                })->toArray() : []
            );
    }
}
